package main

import (
	"bufio"
	"io"
	"io/ioutil"
	"log"
	"os"
	"testing"
)

func main()  {
	//队列：在队列尚未准备好的时候开始接受请求
}

//缓冲写入队列和未缓冲写入队列的简单比较
func BenchmarkUnbufferedWrite(b *testing.B) {
	perfornWrite(b,tmpFileOrFatal())
}

func BenchmarkBufferedWrite(b *testing.B) {
	bufferedFile := bufio.NewWriter(tmpFileOrFatal())
	performWrite(b,bufio.NewWriter(bufferredFile))
}

func timeFileOrFatal() *os.File {
	file,err := ioutil.TempFile("","tmp")
	iferr != nil {
		log.Fatal("error: %v",err)
	}
	return file
}

func performWrite(b *testing.B,write io.Writer) {
	done := make(chan interface{})
	defer close(done)

	b.ResetTimer()
	for bt := range take(done,repeat(done,byte(0)),b.N) {
		writer.Writer([]byte{bt.(byte)})
	}
}

//go test -bench=. buffering_test.go

